package com.hy.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(jwtInterceptor)
//                .addPathPatterns("/api/**")
//                .excludePathPatterns(
//                        "/api/auth/login",
//                        "/api/auth/register",
//                        "/api/products",
//                        "/api/products/*",
//                        "/swagger-ui/**",
//                        "/api-docs/**",
//                        "/admin/**"
//                );
//    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")  // 允许所有域名
                .allowedMethods("GET", "POST", "PUT", "DELETE")  // 允许的请求方法
                .allowedHeaders("*");  // 允许所有请求头
    }

    @Value("${upload.teacher-avatar-path}")
    private String avatarPath;

    @Value("${upload.weekly-pdf-path}")
    private String pdfPath;

    @Value("${upload.banner-path}")
    private String bannerPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 教师头像资源映射
        registry.addResourceHandler("/teacher-avatar/**")
                .addResourceLocations("file:" + avatarPath);
        //pdf文件映射
        registry.addResourceHandler("/weekly-pdf/**")
                .addResourceLocations("file:" + pdfPath);
        // 轮播图资源映射
        registry.addResourceHandler("/banners/**")
                .addResourceLocations("file:" + bannerPath);

    }
} 
